using System.Text;
using Gateway.IQuery;
using Gateway.IQuery.Project;
using Gateway.IQuery.Project.Request;
using Gateway.IQuery.Project.Result;
using Gateway.IRepositories;
using Gateway.SqlSugar;
using SqlSugar;

namespace Gateway.Query;

/// <summary>
/// 
/// </summary>
public class ProjectQuery : IProjectQuery
{
    private readonly IQueryRepository _repository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    public ProjectQuery(IQueryRepository repository)
    {
        _repository = repository;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<PageResult<ProjectPageListResult>> GetPageList(ProjectPageListRequest request)
    {
        StringBuilder where = new StringBuilder(100);
        SugarParameters parameters = new SugarParameters();
        if (!string.IsNullOrEmpty(request.ProjectName))
        {
            where.Append(" AND p.ProjectName like @projectName");
            parameters.Add("@projectName", $"%{request.ProjectName}%");
        }

        if (!string.IsNullOrEmpty(request.ProjectCode))
        {
            where.Append(" AND p.ProjectCode like @ProjectCode");
            parameters.Add("@ProjectCode", $"%{request.ProjectCode}%");
        }

        if (!string.IsNullOrEmpty(request.CreateStartTime))
        {
            where.Append(" AND p.CreatedTime>=@CreateStartTime");
            parameters.Add("@CreateStartTime", request.CreateStartTime);
        }

        if (!string.IsNullOrEmpty(request.CreateEndTime))
        {
            where.Append(" AND p.CreatedTime<=@CreateEndTime");
            parameters.Add("@CreateEndTime", request.CreateEndTime);
        }

        if (!string.IsNullOrEmpty(request.ModifyStartTime))
        {
            where.Append(" AND p.LastModifiedTime>=@ModifyStartTime");
            parameters.Add("@ModifyStartTime", request.ModifyStartTime);
        }

        if (!string.IsNullOrEmpty(request.ModifyEndTime))
        {
            where.Append(" AND p.LastModifiedTime<=@ModifyEndTime");
            parameters.Add("@ModifyEndTime", request.ModifyEndTime);
        }

        if (!string.IsNullOrEmpty(request.Disable))
        {
            where.Append(" AND p.Disabled=@Disable");
            parameters.Add("@Disable", request.Disable);
        }

        int count = 0;
        if (request.Page == 1)
        {
            count = await _repository.QueryFirstOrDefaultAsync<int>(
                $@"select count(1) from  Project p where Deleted='N' {where.ToString()}", parameters);
        }

        string sql =
            $@"select ProjectId,ProjectName,ProjectCode,SeqNo,CreatedTime,LastModifiedTime,Disabled from  Project p where Deleted='N'
{where.ToString()}
order by  ProjectId,CreatedTime
limit  {request.Offset},{request.PageSize}";
        var data = await _repository.QueryAsync<ProjectPageListResult>(sql, parameters);
        return PageResult<ProjectPageListResult>.Create(data.ToList(), count);
    }
}